package com.example.wangyu1.mytextviewdemo;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private String str = "在实际开发过程中由于屏幕宽度不一致，可能会导致在宽度较小的屏幕上文本可能会过长";
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = findViewById(R.id.textview);
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        mTextView.measure(w, h);
        int width = mTextView.getMeasuredWidth();
        int height = mTextView.getMeasuredHeight();
        Log.i("kingiyu", "MainActivity-->-->onCreate-->" + width + "->height->" + height);
        ViewTreeObserver viewTreeObserver = mTextView.getViewTreeObserver();
        viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int height = mTextView.getMeasuredHeight();
                int width = mTextView.getMeasuredWidth();
                Log.i("kingiyu", "MainActivity-->-->onPreDraw-->" + width + "->height:->" + height);
                return true;
            }
        });
       final ViewTreeObserver viewTreeObserver2 = mTextView.getViewTreeObserver();
        viewTreeObserver2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                viewTreeObserver2.removeGlobalOnLayoutListener(this);
                Log.i("kingiyu", "MainActivity-->-->onGlobalLayout-->" + mTextView.getWidth() + "->height->" + mTextView.getHeight());
            }
        });
    }


    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        int height = mTextView.getHeight();
        int width = mTextView.getWidth();
        Log.i("kingiyu", "MainActivity-->-->onWindowFocusChanged-->" + width + "->height:->" + height);
    }
}